package com.yixun.controller;

import com.yixun.entity.YxRole;
import com.yixun.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list")
    public String list(){
        return "org/role/roleList";
    }

    @RequestMapping(value = "/go_select_role")
    public String go_select_role(@RequestParam(value = "user_id") String user_id, Model model){
        model.addAttribute("user_id", user_id);
        return "org/role/select_role";
    }

    /**
     * 获取角色列表
     */
    @RequestMapping(value = "/getAllRoleList")
    @ResponseBody
    public List<YxRole> getAllRoleList(){
        return roleService.getAllRoleList();
    }

    /**
     * 获取角色列表
     */
    @RequestMapping(value = "/getRoleList")
    @ResponseBody
    public List<YxRole> getRoleList(@RequestBody Map map){
        return  roleService.getRoleList(map);
    }

    /**
     * 获取角色列表条数
     */
    @RequestMapping(value = "/getRoleCount")
    @ResponseBody
    public String getRoleCount(@RequestBody Map map){
        return roleService.getRoleCount(map);
    }

    @RequestMapping(value = "/go_role_add")
    public String go_role_add(String role_id, Model model){
        model.addAttribute("role_id", role_id);
        return "org/role/role_add";
    }

    /**
     * 添加角色
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YxRole role){
        int row = roleService.insertRole(this.getSessionUser(), role);
        return row == 1;
    }

    /**
     * 根据角色id获取角色
     */
    @RequestMapping(value = "/getRoleBean/{role_id}")
    @ResponseBody
    public YxRole getRoleBean(@PathVariable(value = "role_id") String role_id){
        return roleService.getRoleBean(role_id);
    }

    /**
     * 修改角色信息
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YxRole role){
        int row = roleService.updateRole(this.getSessionUser(), role);
        return row == 1;
    }

    /**
     * 删除角色
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(@RequestBody Map map){
        int row = roleService.delRole(this.getSessionUser(), map);
        return row > 0;
    }


}
